<template>
  <div class="seller" ref="sellerscroll">
    <div class="seller-content">
      <div class="seller-title">
        <div class="details border-1px">
          <div class="favorite" @click="favoriteToggle" :class="{'active': favorite}">
            <div class="icon icon-favorite"></div>
            <div class="text">{{favoriteText}}</div>
          </div>
          <div class="name">{{seller.name}}</div>
          <div class="score-wrapper">
            <star class="star" :size="36" :score="seller.score"></star>
            <span class="ratingcount">({{seller.ratingCount}})</span>
            <span class="sellcount">月售{{seller.sellCount}}单</span>
          </div>
        </div>
      </div>
      <div class="overview">
        <div class="item border">
          <div class="title">起送价</div>
          <div class="text">
            <span class="big">{{seller.minPrice}}</span>元
          </div>
        </div>
        <div class="item border">
          <div class="title">商家配送</div>
          <div class="text">
            <span class="big">{{seller.deliveryPrice}}</span>元
          </div>
        </div>
        <div class="item">
          <div class="title">平均配送时间</div>
          <div class="text">
            <span class="big">{{seller.deliveryTime}}</span>分钟
          </div>
        </div>
      </div>
      <split></split>
      <div class="content">
        <div class="title">公告与活动</div>
        <p class="bulletin">{{seller.bulletin}}</p>
        <ul class="supports">
          <li class="supports-item border-top-1px" v-for="item in seller.supports" :key="item.type">
            <support-ico class="icon" :size="4" :type="item.type"></support-ico>
            <span class="text">{{item.description}}</span>
          </li>
        </ul>
      </div>
      <split></split>
      <div class="content">
        <div class="title">商家实景</div>
        <div class="pic-wrapper" ref="sellerpic">
          <ul class="pic-list" ref="piclist">
            <li class="pic-item" v-for="pic in seller.pics" :key="pic.id">
              <img :src="pic">
            </li>
          </ul>
        </div>
      </div>
      <split></split>
      <div class="content">
        <div class="title">商家信息</div>
        <ul class="info">
          <li class="info-item border-top-1px" v-for="item in seller.infos" :key="item.id">{{item}}</li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import BScroll from 'better-scroll'
import { saveToLocal, loadFromLocal } from 'common/js/store.js'
import star from '../star/star'
import split from '../split/split'
import supportIco from '../support-ico/support-ico'

export default {
  props: {
    seller: {
      type: Object
    }
  },
  data() {
    return {
      favorite: (() => {
        return loadFromLocal(this.seller.id, 'favorite', false)
      })()
    }
  },
  computed: {
    favoriteText() {
      return this.favorite ? '已收藏' : '收藏'
    }
  },
  // created() {
  //   this.$nextTick(() => {
  //     if (this.seller !== null && this.seller !== {}) {
  //       this._initScroll()
  //     }
  //   })
  // },
  watch: {
    'seller' () {
      this._initScroll()
      this._initPics()
    }
  },
  mounted() {
    this._initScroll()
    this._initPics()
  },
  methods: {
    _initScroll() {
      if (!this.sellerScroll) {
        this.sellerScroll = new BScroll(this.$refs.sellerscroll, {
          click: true
        })
      } else {
        this.sellerScroll.refresh()
      }
    },
    _initPics() {
      if (this.seller.pics) {
        let picWidth = 120
        let margin = 6
        let width = (picWidth + margin) * this.seller.pics.length - margin
        this.$refs.piclist.style.width = width + 'px'
        this.$nextTick(() => {
          if (!this.sellerpicScroll) {
            this.sellerpicScroll = new BScroll(this.$refs.sellerpic, {
              scrollX: true,
              eventPassthrough: 'vertical'
            })
          } else {
            this.sellerpicScroll.refresh()
          }
        })
      }
    },
    favoriteToggle() {
      if (!event._constructed) {
        return
      }
      this.favorite = !this.favorite
      saveToLocal(this.seller.id, 'favorite', this.favorite)
    }
  },
  components: {
    star,
    split,
    'support-ico': supportIco
  }
}

</script>

<style lang='stylus' scoped>
@import "../../common/stylus/mixin.styl"
@import "../../common/stylus/variable.styl"

.seller
  position: absolute
  top: 174px
  bottom: 0
  left: 0
  width: 100%
  overflow: hidden
  .seller-title
    padding: 0 18px
    .details
      padding: 18px 0
      position: relative
      border-1px($color-row-line)
      .favorite
        position: absolute
        right: 0
        bottom: 18px
        width: 40px
        text-align: center
        .icon
          margin-bottom: 4px
          line-height: 24px
          font-size: 24px
          color: $color-gray2
        .text
          line-height: 10px
          font-size: 10px
          color: $color-gray2
        &.active
          .icon
            color: $color-red
          .text
            color: $color-gray
      .name
        margin-bottom: 8px
        line-height: 14px
        font-size: 14px
        color: $color-background
      .score-wrapper
        font-size: 0
        .star
          display: inline-block
          margin-right: 8px
        .ratingcount
          margin-right: 12px
        .ratingcount,.sellcount
          vertical-align: top
          line-height: 15px
          font-size: 10px
          color: $color-gray
  .overview
    display: flex
    padding: 18px 0
    .border
      border-right-1px($color-row-line)
    .item
      flex: 1
      text-align: center
      .title
        margin-bottom: 4px
        line-height: 10px
        font-size: 10px
        color: $color-gray2
      .text
        line-height: 24px
        font-size: 10px
        font-weight: 200
        color: $color-background
        .big
          font-size: 24px
  .content
    padding: 18px
    .title
      line-height: 14px
      font-size: 14px
      color: $color-background
    .bulletin
      padding: 8px 0 0 12px
      line-height: 24px
      font-size: 12px
      font-weight: 200
      color: $color-red
    .supports
      margin-top: 16px
      .supports-item
        padding: 16px 12px
        border-top-1px($color-row-line)
        .icon
          margin-right: 6px
          vertical-align: top
          line-height 16px
        .text
          vertical-align: top
          line-height 16px
          font-size: 12px
          font-weight: 200
          color: $color-background
    .pic-wrapper
      margin-top: 12px
      overflow: hidden
      height: 90px
      white-space: nowrap;
      .pic-item
        margin-right: 6px
        display: inline-block
        width: 120px
        height: 90px
        img
          width: 120px
          height: 90px
      .pics-item:last-child
        margin-right: none
    .info
      margin-top: 12px
      .info-item
        padding: 16px 12px
        line-height 16px
        font-size: 12px
        font-weight: 200
        color: $color-background
        border-top-1px($color-row-line)
</style>
